import numpy as np
import csv

window_size = 10
deg = 3
result = []
with open('csv_results/all.csv', newline='', encoding='utf-8') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    reader.__next__()
    tmp = []
    for row in reader:
        tmp.append([float(x) for x in row[1:]])
    matrix = np.array(tmp).transpose()
    # print(matrix.transpose())

    serial = 1
    for sdg in matrix:
        data = sdg[:]
        for year in range(2022, 2032):
            x = np.arange(year + 1 - window_size, year + 1)
            y = data[-window_size:]
            p = np.polyfit(x, y, deg)
            data = np.append(data, np.polyval(p, year))
        result.append(f"{','.join([str(x) for x in data])}\n")
        serial += 1
with open("csv_results/mean-predict-new.csv", 'w') as f:
    f.writelines(result)



